package com.xuanzheng.company.servlet;

import com.google.gson.Gson;
import com.xuanzheng.company.entity.Company;
import com.xuanzheng.company.entity.CompanyEmployee;
import com.xuanzheng.company.entity.User;
import com.xuanzheng.company.service.CompanyEmployeeService;
import com.xuanzheng.company.service.CompanyService;
import com.xuanzheng.company.service.impl.CompanyEmployeeServiceImpl;
import com.xuanzheng.company.service.impl.CompanyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/getEmployeeApplications")
public class GetEmployeeApplicationsServlet extends HttpServlet {
    private final CompanyEmployeeService companyEmployeeService = new CompanyEmployeeServiceImpl();
    private final CompanyService companyService = new CompanyServiceImpl();
    private final Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("application/json;charset=UTF-8");
        PrintWriter out = response.getWriter();
        
        Map<String, Object> result = new HashMap<>();
        
        try {
            // 获取当前用户
            HttpSession session = request.getSession();
            User user = (User) session.getAttribute("user");
            
            if (user == null) {
                result.put("success", false);
                result.put("message", "用户未登录");
                out.write(gson.toJson(result));
                return;
            }
            
            // 检查用户是否是公司负责人
            if (user.getRole() != 1) {
                result.put("success", false);
                result.put("message", "只有公司负责人可以查看入职申请");
                out.write(gson.toJson(result));
                return;
            }
            
            // 获取公司ID
            Company company = companyService.getCompanyByManagerId(user.getId());
            if (company == null) {
                result.put("success", false);
                result.put("message", "您还没有创建公司");
                out.write(gson.toJson(result));
                return;
            }
            
            // 检查是否有ID参数，如果有则获取单个申请
            String applicationId = request.getParameter("id");
            if (applicationId != null && !applicationId.trim().isEmpty()) {
                CompanyEmployee application = companyEmployeeService.getApplicationById(applicationId);
                
                if (application == null) {
                    result.put("success", false);
                    result.put("message", "找不到指定的申请记录");
                } else {
                    // 检查申请是否属于当前公司
                    if (!application.getCompanyId().equals(company.getId())) {
                        result.put("success", false);
                        result.put("message", "无权查看该申请记录");
                    } else {
                        result.put("success", true);
                        result.put("application", application);
                        result.put("company", company);
                    }
                }
                
                out.write(gson.toJson(result));
                return;
            }
            
            // 获取状态参数
            String statusStr = request.getParameter("status");
            int status = -1;
            if (statusStr != null && !statusStr.trim().isEmpty()) {
                try {
                    status = Integer.parseInt(statusStr);
                } catch (NumberFormatException e) {
                    // 忽略解析错误，默认获取所有状态
                }
            }
            
            // 获取申请列表
            List<CompanyEmployee> applications;
            if (status >= 0) {
                applications = companyEmployeeService.getApplicationsByCompanyIdAndStatus(company.getId(), status);
            } else {
                applications = companyEmployeeService.getApplicationsByCompanyId(company.getId());
            }
            
            result.put("success", true);
            result.put("applications", applications);
            result.put("company", company);
        } catch (Exception e) {
            e.printStackTrace();
            result.put("success", false);
            result.put("message", "获取入职申请列表失败：" + e.getMessage());
        }
        
        out.write(gson.toJson(result));
        out.flush();
    }
} 